package org.study.redisson.boot.config;

import org.redisson.api.RedissonClient;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 基于 Redisson 的缓存配置
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年01月19日
 */
@Configuration
public class MyCacheConfig {
    @Bean
    public CacheManager cacheManager(RedissonClient redissonClient) {
        // 为Redisson的缓存管理器设置参数：缓存数据有效期为12分钟、最长空闲时间为10分钟
        CacheConfig conf = new CacheConfig(12 * 60 * 1000, 10 * 60 * 1000);
        HashMap<String, CacheConfig> map = new HashMap<>() {{
            this.put("userCache", conf);
        }};
        RedissonSpringCacheManager manager = new RedissonSpringCacheManager(redissonClient, map);
        return manager;
    }
}
